import 'package:family_expense/features/income_expense/models/income_expense_detail.dart';
import 'package:family_expense/features/income_expense_analysis/models/income_expense_annual_summry.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class IncomeExpenseAnalysisService {
  final String _url = '/income-expense-analysis';
  final HttpRequestService _httpRequestService = HttpRequestService();

  /// Retrieves current user account group all distinct years of income/expense from backend service.
  Future<List<int>> queryExistingDistinctYears() async {
    // Gets the response of the http get request.
    final response =
        await _httpRequestService.get('$_url/query-existing-distinct-years');

    // Parses the data from [Map<String, dynamic>] to int list.
    return List<int>.from(response.payload);
  }

  /// Retrieves current user account group annual summary of current all existing years from backend service.
  Future<List<IncomeExpenseAnnualSummary>> querySummaryByExistingYears() async {
    // Gets the response of the http get request.
    final response =
        await _httpRequestService.get('$_url/query-summary-by-existing-years');

    // Parses the data from [Map<String, dynamic>] to [IncomeExpenseDetail].
    return (response.payload as List<dynamic>)
        .map((p) => IncomeExpenseAnnualSummary.fromJson(p))
        .toList();
  }

  /// Retrieves current user account group overall summary from backend service.
  Future<Map<String, dynamic>> queryOverallSummary() async {
    // Gets the response of the http get request.
    final response =
        await _httpRequestService.get('$_url/query-overall-summary');

    // Returns [Map<String, dynamic>].
    return response.payload;
  }

  /// Retrieves current user account group annual summary by month from backend service.
  Future<Map<String, dynamic>> queryAnnualMonthlySummary(int? year) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService
        .get('$_url/query-annual-monthly-summary', params: {'year': year});

    // Returns [Map<String, dynamic>].
    return response.payload;
  }

  /// Retrieves current user account group annual summary by month from backend service.
  Future<Map<String, dynamic>> queryMonthlyDailySummary(
      int? year, int? month) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService.get(
        '$_url/query-monthly-daily-summary',
        params: {'year': year, 'month': month});

    // Returns [Map<String, dynamic>].
    return response.payload;
  }

  /// Retrieves current user account group [IncomeExpenseDetail] list from backend service.
  Future<List<IncomeExpenseDetail>> queryTopRankByAccountGroup(
      int pageSize,
      int pageIndex,
      String type,
      String paymentType,
      int? year,
      int? month) async {
    // Gets the response of the http get request.
    // Formats the date parameter to clear the time value.
    final response = await _httpRequestService
        .get('$_url/query-top-rank-by-account-group', params: {
      'pageSize': pageSize,
      'pageIndex': pageIndex,
      'type': type,
      'paymentType': paymentType,
      'year': year,
      'month': month,
    });

    // Parses the data from [Map<String, dynamic>] to [IncomeExpenseDetail].
    return (response.payload as List<dynamic>)
        .map((p) => IncomeExpenseDetail.fromJson(p))
        .toList();
  }

  /// Retrieves current user account group annual amount by category from backend service.
  Future<List<Map<String, dynamic>>> querySummaryByDetailType(
      int? year, int? month, String type, String paymentType) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService
        .get('$_url/query-summary-by-detail-type', params: {
      'year': year,
      'month': month,
      'type': type,
      'paymentType': paymentType
    });

    return List<Map<String, dynamic>>.from(response.payload);
  }
}
